﻿#include "pch.h"
#if _QG_USE_GL
#include "qgdrv_gl_rdh.h"

#if QG_OPENGL_V4

//
#define QGGL_IMPL_ENTRYPOINT(type,func)		type func;
#define QGGL_TEST_ENTRYPOINT(type,func)		if (func == NULL) return false;
#define QGGL_PRINT_ENTRYPOINT(type,func)	k_mesgf("%s -> %s\n", K_STRING(func), func!=NULL ? "ok" : "fail");
#if _QG_USE_SDL
#define QGGL_GET_PROC_ADDRESS(type,func)	func = (type)SDL_GL_GetProcAddress(K_STRING(func));
#elif _SB_WINDOWS_
#define QGGL_GET_PROC_ADDRESS(type,func)	func = (type)wglGetProcAddress(K_STRING(func));
#elif _SB_LINUX_ || _SB_BSD_
#define QGGL_GET_PROC_ADDRESS(type,func)	func = (type)glXGetProcAddressARB(K_STRING(func));
#else
#error fix me
#endif

//
QGGL_ENTRYPOINT_LIST(QGGL_IMPL_ENTRYPOINT);

//
bool qbOpenGLv4::gl_shader_language_100 = false;
GLint qbOpenGLv4::qgl_max_off_screen_count = 1;

//
void qbOpenGLv4::QglInitializeExtensions(const char* extstr)
{
	qbOpenGLBase::QglInitializeExtensions(extstr);

	// 확장 기능
	gl_shader_language_100 = k_stristr(extstr, "GL_ARB_shading_language_100") != 0;

	// 그리기 버퍼 가능
	if (k_stristr(extstr, "GL_ARB_DRAW_BUFFERS") != NULL)
	{
		// 오프 스크린
		QglGetIntegerv(GL_MAX_DRAW_BUFFERS, &qgl_max_off_screen_count, 0);

		if (qgl_max_off_screen_count > QGSGOFF_MAX_VALUE)
			qgl_max_off_screen_count = QGSGOFF_MAX_VALUE;
	}

	// 함수 포인터
	static bool sInitFunc = false;

	if (!sInitFunc)
	{
		sInitFunc = true;

		// 공통 밖에 없나부...
		QGGL_ENTRYPOINT_LIST(QGGL_GET_PROC_ADDRESS);
	}

	// 카파
	kany a;

	// 버전
	a.cp = glGetString(GL_VERSION);
	a.f = k_atof((const char*)a.s);
	qgl_version = (GLint)(k_floor(a.f) * 100.0f + k_ceil(k_fract(a.f) * 10.0f));

	// 세이더 버전
	if (gl_shader_language_100 || qgl_version >= 200)
	{
		// 여기는 무조건 지원해야함
		a.cp = glGetString(GL_SHADING_LANGUAGE_VERSION);

		if (glGetError() == GL_INVALID_ENUM)
			qgl_shader_version = 100;
		else
		{
			a.f = k_atof((const char*)a.s);
			qgl_shader_version = (kushort)(k_floor(a.f) * 100.0f + k_ceil(k_fract(a.f) * 10.0f));
		}
	}
}

//
bool qbOpenGLv4::QglTestExtensions()
{
	if (!qbOpenGLBase::QglTestExtensions())
		return false;

#if 0 || _SB_DEBUG_
	QGGL_ENTRYPOINT_LIST(QGGL_PRINT_ENTRYPOINT);
#endif

	QGGL_ENTRYPOINT_LIST(QGGL_TEST_ENTRYPOINT);

	return true;
}

#endif	// QG_OPENGL_V4

#endif	// _QG_USE_GL
